var app = getApp();

import TWEEN from './tween.js';

var ZW_Ctx;
var angle = 0;
var ZW_PicArray = [];
var goodsArray = [];
var ZW_Offset = 30;
var ZW_PointsArray = [];
var ZW_Info;
var ZW_PixelRatio = 2;
var gIsIphone = false;

var ZW_ClickNodeName = '';
var ZW_HoldNodeName = '';       //长按得珠子名称
var ZW_HoldIndex = 0;   //长按得珠子所在的索引值
var ZW_bHold = false;
var ZW_bLoading = false;  //是否正在加载图片

var Scene_OffsetX = 0;
var Scene_OffsetY = 0;

var Scene_ScaleX = 1.0;
var Scene_ScaleY = 1.0;

var ZW_ClickPos;

var ZW_CurrentPos = 0;

var ZW_Close_Url = '../../img/close.png';
var ZW_Scale_Url = '../../img/scale.png';
var ZW_Rot_Url = '../../img/rotation.png';

var ZW_bTipEdit = true;

var ZW_MoveType = '';

function guid() {
        return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
                var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
                return v.toString(16);
        });
}


function AddChain(tag, url) {
        //AddImage(tag,url,ZW_Info.windowWidth/2,150,200,200,0,1.0);

        AddImage(tag, url, 0, 100, ZW_Info.windowHeight * 0.7882, ZW_Info.windowHeight, 0, 1.0);
}



function AddImage(tag, url, x, y, width, height, rot, scale, visible, order) {
        if (!order) order = 0;
        let contentPicNetPath = url;

        var cImage = { tag: tag, x: x, y: y, width: width, height: height, url: '', rot: rot, scale: scale, visible: visible, renderOrder: order };
        ZW_PicArray.push(cImage);

        jd.getImageInfo({
                //保存网络图片
                src: contentPicNetPath,
                success:
                        function (res) {
                                cImage.url = res.path;

                                //ZW_PicArray.push({tag:tag,x:x,y:y,width:width,height:height,url:res.path,rot:rot,scale:scale});
                                //console.log(ZW_PicArray);
                        }
        })
}


function AddExpression(name) {

        for (var i = 0; i < 5; i++) {
                RemoveImage('expression-' + i);
        }

        //   AddImage('expression1', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/expression/ex2/female02-0.png', ZW_Info.windowWidth * 0.5, ZW_Info.windowWidth * 0.85,
        //   ZW_Info.windowWidth * 0.5 * 1.4, ZW_Info.windowWidth * 1.4, 0, 1.0,true);

        var gifUrlArray = [];
        for (var i = 0; i < 5; i++) {
                gifUrlArray.push(name + '-' + i + '.png');

                if (i == 0) {
                        AddGifImage1('expression-' + i, gifUrlArray[i], ZW_Info.windowWidth * 0.526, ZW_Info.windowWidth * 0.462,
                                ZW_Info.windowWidth * 0.5333 * 0.55, ZW_Info.windowWidth * 0.42666 * 0.55, 0, 1.0, false, 4);
                } else {
                        AddGifImage1('expression-' + i, gifUrlArray[i], ZW_Info.windowWidth * 0.526, ZW_Info.windowWidth * 0.462,
                                ZW_Info.windowWidth * 0.5333 * 0.55, ZW_Info.windowWidth * 0.42666 * 0.55, 0, 1.0, false, 4);
                }

        }
}

//判断2d点是否在三角形中。x0, y0，为检测点。剩余三个点，为三角形的三个点。当返回结果为true，则检测点在三角形上。如果为false，则点不在三角形上。
function PointInTriangle(x0, y0, x1, y1, x2, y2, x3, y3) {
        var divisor = (y2 - y3) * (x1 - x3) + (x3 - x2) * (y1 - y3);
        var a = ((y2 - y3) * (x0 - x3) + (x3 - x2) * (y0 - y3)) / divisor;
        var b = ((y3 - y1) * (x0 - x3) + (x1 - x3) * (y0 - y3)) / divisor;
        var c = 1 - a - b;

        return a >= 0 && a <= 1 && b >= 0 && b <= 1 && c >= 0 && c <= 1;
}

//通过坐标点返回点击的类型
function GetTipBtnType(index, x0, y0) {
        for (var i = 0; i < ZW_PicArray.length; i++) {
                if (ZW_PicArray[i].tag.indexOf('tip' + index) > -1) {
                        var posX = ZW_PicArray[i].x0;
                        var posY = ZW_PicArray[i].y0;
                        var width = ZW_PicArray[i].width * ZW_PicArray[i].scale0;
                        var height = ZW_PicArray[i].height * ZW_PicArray[i].scale0;
                        var pDis = Math.sqrt((width * width) + (height * height));

                        var rad1 = Math.asin(height / pDis) + Math.PI * ZW_PicArray[i].rot0 / 180.0;

                        var posX1 = posX - 0.5 * pDis * Math.cos(rad1);
                        var posY1 = posY - 0.5 * pDis * Math.sin(rad1);

                        var rad2 = (Math.PI - Math.asin(height / pDis)) + Math.PI * ZW_PicArray[i].rot0 / 180.0;

                        var posX2 = posX - 0.5 * pDis * Math.cos(rad2);
                        var posY2 = posY - 0.5 * pDis * Math.sin(rad2);

                        var rad3 = (Math.PI + Math.asin(height / pDis)) + Math.PI * ZW_PicArray[i].rot0 / 180.0;

                        var posX3 = posX - 0.5 * pDis * Math.cos(rad3);
                        var posY3 = posY - 0.5 * pDis * Math.sin(rad3);

                        var rad4 = (2 * Math.PI - Math.asin(height / pDis)) + Math.PI * ZW_PicArray[i].rot0 / 180.0;

                        var posX4 = posX - 0.5 * pDis * Math.cos(rad4);
                        var posY4 = posY - 0.5 * pDis * Math.sin(rad4);


                        var pDis1 = Math.sqrt(((x0 - posX1) * (x0 - posX1)) + ((y0 - posY1) * (y0 - posY1)));
                        var pDis2 = Math.sqrt(((x0 - posX2) * (x0 - posX2)) + ((y0 - posY2) * (y0 - posY2)));
                        var pDis3 = Math.sqrt(((x0 - posX3) * (x0 - posX3)) + ((y0 - posY3) * (y0 - posY3)));

                        if (pDis1 < 30) {
                                return 'remove';
                        } else if (pDis2 < 30) {
                                return 'scale';
                        } else if (pDis3 < 30) {
                                return 'rotation';
                        } else {
                                return '';
                        }

                        //return [{x:posX1,y:posY1},{x:posX2,y:posY2},{x:posX3,y:posY3},{x:posX4,y:posY4}];
                }
        }
}

//获取贴纸的四个点的位置
function GetTipPoints(index) {
        for (var i = 0; i < ZW_PicArray.length; i++) {
                if (ZW_PicArray[i].tag.indexOf('tip' + index) > -1) {
                        var posX = ZW_PicArray[i].x0;
                        var posY = ZW_PicArray[i].y0;
                        var width = ZW_PicArray[i].width * ZW_PicArray[i].scale0;
                        var height = ZW_PicArray[i].height * ZW_PicArray[i].scale0;
                        var pDis = Math.sqrt((width * width) + (height * height));

                        var rad1 = Math.asin(height / pDis) + Math.PI * ZW_PicArray[i].rot0 / 180.0;

                        var posX1 = posX - 0.5 * pDis * Math.cos(rad1);
                        var posY1 = posY - 0.5 * pDis * Math.sin(rad1);

                        var rad2 = (Math.PI - Math.asin(height / pDis)) + Math.PI * ZW_PicArray[i].rot0 / 180.0;

                        var posX2 = posX - 0.5 * pDis * Math.cos(rad2);
                        var posY2 = posY - 0.5 * pDis * Math.sin(rad2);

                        var rad3 = (Math.PI + Math.asin(height / pDis)) + Math.PI * ZW_PicArray[i].rot0 / 180.0;

                        var posX3 = posX - 0.5 * pDis * Math.cos(rad3);
                        var posY3 = posY - 0.5 * pDis * Math.sin(rad3);

                        var rad4 = (2 * Math.PI - Math.asin(height / pDis)) + Math.PI * ZW_PicArray[i].rot0 / 180.0;

                        var posX4 = posX - 0.5 * pDis * Math.cos(rad4);
                        var posY4 = posY - 0.5 * pDis * Math.sin(rad4);


                        // jd.showToast({
                        //   title: (posX-width*0.5).toString() +','+ (posY-height*0.5).toString()+','+posX1.toString() +','+ posY1.toString(),
                        //   icon: 'success',
                        //   duration: 1500
                        // })

                        // jd.showToast({
                        //   title: (posX-width*0.5).toString() +','+ (posY+height*0.5).toString()+','+posX4.toString() +','+ posY4.toString(),
                        //   icon: 'success',
                        //   duration: 1500
                        // })

                        return [{ x: posX1, y: posY1 }, { x: posX2, y: posY2 }, { x: posX3, y: posY3 }, { x: posX4, y: posY4 }];
                }
        }
}


//设置场景中的贴纸是否编辑
function SetEditState(bEdit) {
        ZW_bTipEdit = bEdit;
}

//获取贴纸的位置
function GetTipPosition(index) {
        for (var i = 0; i < ZW_PicArray.length; i++) {
                if (ZW_PicArray[i].tag.indexOf('tip' + index) > -1) {
                        return [ZW_PicArray[i].x0, ZW_PicArray[i].y0];
                }
        }
}

//设置贴纸的位置
function SetTipPosition(index, x, y) {
        for (var i = 0; i < ZW_PicArray.length; i++) {
                if (ZW_PicArray[i].tag.indexOf('tip' + index) > -1) {
                        ZW_PicArray[i].x0 = x;
                        ZW_PicArray[i].y0 = y;
                }
        }
}

//获取贴纸的旋转值
function GetTipRotation(index) {
        for (var i = 0; i < ZW_PicArray.length; i++) {
                if (ZW_PicArray[i].tag.indexOf('tip' + index) > -1) {
                        return ZW_PicArray[i].rot0;
                }
        }
}

//设置贴纸的旋转值
function SetTipRotation(index, rot) {
        for (var i = 0; i < ZW_PicArray.length; i++) {
                if (ZW_PicArray[i].tag.indexOf('tip' + index) > -1) {
                        ZW_PicArray[i].rot0 = rot;

                }
        }
}

//获取贴纸的缩放
function GetTipScale(index) {
        for (var i = 0; i < ZW_PicArray.length; i++) {
                if (ZW_PicArray[i].tag.indexOf('tip' + index) > -1) {
                        return ZW_PicArray[i].scale0;
                }
        }
}

//设置贴纸的缩放
function SetTipScale(index, scale) {
        for (var i = 0; i < ZW_PicArray.length; i++) {
                if (ZW_PicArray[i].tag.indexOf('tip' + index) > -1) {
                        ZW_PicArray[i].scale0 = scale;

                }
        }
}


//删除贴纸
function RemoveTip(index) {
        for (var i = 0; i < ZW_PicArray.length; i++) {
                if (ZW_PicArray[i].tag.indexOf('tip' + index) > -1) {
                        ZW_PicArray.splice(i, 1);
                        i--;
                }
        }
}

//添加贴纸
function AddTip(url, index, num, width, height, x, y) {
        if (!x) x = 0;
        if (!y) y = 0;
        var gifUrlArray = [];
        for (var i = 0; i < num; i++) {
                gifUrlArray.push(url + index + '-' + i + '.png');

                if (i == 0) {
                        AddGifImage2('tip' + index + '-' + i, gifUrlArray[i], x, y,
                                width * 0.3, height * 0.3, 0, 1.0, false, num, 9, index);

                        // AddGifImage2('tip'+index+'-'+i, gifUrlArray[i], width * 0.3, height * 1.3,
                        // width * 0.3 , height * 0.3, 0, 1.0,false,num,9,index);
                } else {
                        AddGifImage2('tip' + index + '-' + i, gifUrlArray[i], x, y,
                                width * 0.3, height * 0.3, 0, 1.0, false, num, 9, index);

                        // AddGifImage2('tip'+index+'-'+i, gifUrlArray[i], width * 0.3, height * 1.3,
                        // width * 0.3, height * 0.3, 0, 1.0,false,num,9,index);
                }

        }
}


function AddBackgroud(type) {
        for (var i = 0; i < 40; i++) {
                RemoveImage('back-' + i);
        }

        RemoveImage('back1');

        if (type == 1) {
                AddGifImage('back1', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/BG1-bottom.jpg', ZW_Info.windowWidth * 0.5, ZW_Info.windowWidth * 1.5 * 1.026667 - 0.1,
                        ZW_Info.windowWidth, ZW_Info.windowWidth * 1.026667, 0, 1.0, true, 0);

                // AddGifImage('back-0', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/1/b' + 0 + '.png', ZW_Info.windowWidth*0.5 , ZW_Info.windowWidth *0.5* 1.026667,
                // ZW_Info.windowWidth , ZW_Info.windowWidth * 1.026667, 0, 1.0,false);

                var gifUrlArray = [];
                for (var i = 0; i < 40; i++) {
                        gifUrlArray.push('https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/1/b' + i + '.png');

                        if (i == 0) {
                                AddGifImage('back-' + i, gifUrlArray[i], ZW_Info.windowWidth * 0.5, ZW_Info.windowWidth * 0.5 * 1.026667,
                                        ZW_Info.windowWidth, ZW_Info.windowWidth * 1.026667, 0, 1.0, true, 0);
                        } else {
                                AddGifImage('back-' + i, gifUrlArray[i], ZW_Info.windowWidth * 0.5, ZW_Info.windowWidth * 0.5 * 1.026667,
                                        ZW_Info.windowWidth, ZW_Info.windowWidth * 1.026667, 0, 1.0, false, 0);
                        }


                }




        } else if (type == 2) {
                AddGifImage('back1', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/BG2-bottom.jpg', ZW_Info.windowWidth * 0.5, ZW_Info.windowWidth * 1.5 * 1.026667 - 0.1,
                        ZW_Info.windowWidth, ZW_Info.windowWidth * 1.026667, 0, 1.0, true, 0);

                var gifUrlArray = [];
                for (var i = 0; i < 40; i++) {
                        gifUrlArray.push('https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/2/b' + i + '.png');

                        if (i == 0) {
                                AddGifImage('back-' + i, gifUrlArray[i], ZW_Info.windowWidth * 0.5, ZW_Info.windowWidth * 0.5 * 1.026667,
                                        ZW_Info.windowWidth, ZW_Info.windowWidth * 1.026667, 0, 1.0, true, 0);
                        } else {
                                AddGifImage('back-' + i, gifUrlArray[i], ZW_Info.windowWidth * 0.5, ZW_Info.windowWidth * 0.5 * 1.026667,
                                        ZW_Info.windowWidth, ZW_Info.windowWidth * 1.026667, 0, 1.0, false, 0);
                        }


                }



        } else if (type == 3) {
                AddGifImage('back1', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/BG3-bottom.jpg', ZW_Info.windowWidth * 0.5, ZW_Info.windowWidth * 1.5 * 1.026667 - 0.1,
                        ZW_Info.windowWidth, ZW_Info.windowWidth * 1.026667, 0, 1.0, true, 0);

                var gifUrlArray = [];
                for (var i = 0; i < 40; i++) {
                        gifUrlArray.push('https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/3/b' + i + '.png');

                        if (i == 0) {
                                AddGifImage('back-' + i, gifUrlArray[i], ZW_Info.windowWidth * 0.5, ZW_Info.windowWidth * 0.5 * 1.026667,
                                        ZW_Info.windowWidth, ZW_Info.windowWidth * 1.026667, 0, 1.0, true, 0);
                        } else {
                                AddGifImage('back-' + i, gifUrlArray[i], ZW_Info.windowWidth * 0.5, ZW_Info.windowWidth * 0.5 * 1.026667,
                                        ZW_Info.windowWidth, ZW_Info.windowWidth * 1.026667, 0, 1.0, false, 0);
                        }

                }



        } else if (type == 4) {
                AddGifImage('back1', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/BG4-bottom.jpg', ZW_Info.windowWidth * 0.5, ZW_Info.windowWidth * 1.5 * 1.026667 - 0.1,
                        ZW_Info.windowWidth, ZW_Info.windowWidth * 1.026667, 0, 1.0, true, 0);

                var gifUrlArray = [];
                for (var i = 0; i < 40; i++) {
                        gifUrlArray.push('https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/4/b' + i + '.png');

                        if (i == 0) {
                                AddGifImage('back-' + i, gifUrlArray[i], ZW_Info.windowWidth * 0.5, ZW_Info.windowWidth * 0.5 * 1.026667,
                                        ZW_Info.windowWidth, ZW_Info.windowWidth * 1.026667, 0, 1.0, true, 0);
                        } else {
                                AddGifImage('back-' + i, gifUrlArray[i], ZW_Info.windowWidth * 0.5, ZW_Info.windowWidth * 0.5 * 1.026667,
                                        ZW_Info.windowWidth, ZW_Info.windowWidth * 1.026667, 0, 1.0, false, 0);
                        }


                }

        }
}

var Load_URL_Num = 0;

function AddGifImage(tag, url, x, y, width, height, rot, scale, visible, order) {
        let contentPicNetPath = url;

        var cImage = { tag: tag, x: x, y: y, width: width, height: height, url: '', rot: rot, scale: scale, visible: visible, renderOrder: order };
        ZW_PicArray.unshift(cImage);

        Load_URL_Num++;

        jd.getImageInfo({
                //保存网络图片
                src: contentPicNetPath,
                success:
                        function (res) {
                                cImage.url = res.path;

                                Load_URL_Num--;
                                if (Load_URL_Num == 0) {

                                        var coords = { x: 0 };
                                        new TWEEN.Tween(coords)
                                                .to({ x: 39 }, 1000 * 40.0 / 15.0)
                                                .repeat(Infinity)
                                                .easing(TWEEN.Easing.Linear.None)
                                                .onUpdate(function () {
                                                        var num = Math.round(coords.x);
                                                        for (var i = 0; i < 40; i++) {
                                                                SetImageVisible('back-' + i, false);
                                                        }

                                                        SetImageVisible('back-' + num, true);

                                                })
                                                .delay(0)
                                                .start();
                                }
                        }
        })
}

var Load_URL_Num1 = 0;

var G_Count = 0;

function AddGifImage1(tag, url, x, y, width, height, rot, scale, visible, order) {
        let contentPicNetPath = url;

        var cImage = { tag: tag, x: x, y: y, width: width, height: height, url: '', rot: rot, scale: scale, visible: visible, renderOrder: order };
        ZW_PicArray.push(cImage);

        Load_URL_Num1++;

        jd.getImageInfo({
                //保存网络图片
                src: contentPicNetPath,
                success:
                        function (res) {
                                cImage.url = res.path;

                                Load_URL_Num1--;
                                if (Load_URL_Num1 == 0) {

                                        var coords = { x: 4 };
                                        new TWEEN.Tween(coords)
                                                .to({ x: 0 }, 1000 * 5.0 / 15.0)
                                                .repeat(Infinity).repeatDelay(1200)
                                                .easing(TWEEN.Easing.Linear.None)
                                                .onUpdate(function () {
                                                        var num = Math.round(coords.x) % 5;

                                                        for (var i = 4; i > -1; i--) {
                                                                SetImageVisible('expression-' + i, false);
                                                        }

                                                        SetImageVisible('expression-' + num, true);
                                                })
                                                .delay(0)
                                                .start();
                                }
                        }
        })
}

var Load_URL_Num2 = 0;

function AddGifImage2(tag, url, x, y, width, height, rot, scale, visible, num1, order, index) {
        let contentPicNetPath = url;

        //var cImage = { tag: tag, x: x, y: y, width: width, height: height, url: '', rot: rot, scale: scale ,visible:visible,renderOrder:order,type:'tip'};

        var cImage = { tag: tag, x: 0, y: 0, x0: x, y0: y, width: width, height: height, url: '', rot: 0, rot0: rot, scale: scale, scale0: 1.0, visible: visible, renderOrder: order, type: 'tip' };

        ZW_PicArray.push(cImage);

        Load_URL_Num2++;

        jd.getImageInfo({
                //保存网络图片
                src: contentPicNetPath,
                success:
                        function (res) {
                                cImage.url = res.path;

                                //cImage.url = '../../img/close.png';
                                Load_URL_Num2--;
                                if (Load_URL_Num2 == 0) {

                                        var coords = { x: 0 };
                                        new TWEEN.Tween(coords)
                                                .to({ x: num1 }, 1000 * num1 / 4.0)
                                                .repeat(Infinity)
                                                .easing(TWEEN.Easing.Linear.None)
                                                .onUpdate(function () {
                                                        var num = Math.round(coords.x) % num1;

                                                        // jd.showToast({
                                                        //   title: num.toString(),
                                                        //   icon: 'success',
                                                        //   duration: 1500
                                                        // })

                                                        for (var i = 0; i < num1; i++) {
                                                                SetImageVisible('tip' + index + '-' + i, false);
                                                        }

                                                        SetImageVisible('tip' + index + '-' + num, true);

                                                })
                                                .delay(0)
                                                .start();
                                }
                        }
        })
}


function RemoveImage(tag) {
        for (var i = 0; i < ZW_PicArray.length; i++) {
                if (tag == ZW_PicArray[i].tag) {
                        ZW_PicArray.splice(i, 1);
                        i--;
                }
        }
}


function SetImageVisible(tag, visible) {
        for (var i = 0; i < ZW_PicArray.length; i++) {
                if (tag == ZW_PicArray[i].tag) {
                        ZW_PicArray[i].visible = visible;
                }
        }
}


function SetImageScale(tag, scale) {
        for (var i = 0; i < ZW_PicArray.length; i++) {
                if (tag == ZW_PicArray[i].tag) {
                        ZW_PicArray[i].scale = scale;
                }
        }
}

function SetImageUrl(tag, url) {
        let contentPicNetPath = url;

        // var cImage = {tag:tag,x:x,y:y,width:width,height:height,url:'',rot:rot,scale:scale};
        // ZW_PicArray.push(cImage);

        if (url == '') {    //不显示某些部件
                for (var i = 0; i < ZW_PicArray.length; i++) {
                        if (tag == ZW_PicArray[i].tag) {
                                ZW_PicArray[i].url = '';
                        }
                }
        }

        jd.getImageInfo({
                //保存网络图片
                src: contentPicNetPath,
                success:
                        function (res) {
                                for (var i = 0; i < ZW_PicArray.length; i++) {
                                        if (tag == ZW_PicArray[i].tag) {
                                                ZW_PicArray[i].url = res.path;

                                                // jd.showToast({
                                                //   title: '替换',
                                                //   icon: 'success',
                                                //   duration: 1500
                                                // })
                                        }
                                }
                                //cImage.url = res.path;
                                // ZW_PicArray.push({tag:tag,x:x,y:y,width:width,height:height,url:res.path,rot:rot,scale:scale});
                                //console.log(ZW_PicArray);

                        }
        })
}

//松开手指的处理
function ReleaseProcess() {

}

//改变视角
function ChangeView(type) {
        if (type == 'top') {
                var coords = { x: Scene_OffsetX, y: Scene_OffsetY, x1: Scene_ScaleX, y1: Scene_ScaleY };
                new TWEEN.Tween(coords)
                        .to({ x: -ZW_Info.windowWidth * 0.08, y: +ZW_Info.windowWidth * 0.1, x1: 1.2, y1: 1.2 }, 500)
                        .repeat(0)
                        .easing(TWEEN.Easing.Linear.None)
                        .onUpdate(function () {
                                Scene_OffsetX = coords.x;
                                Scene_OffsetY = coords.y;
                                Scene_ScaleX = coords.x1;
                                Scene_ScaleY = coords.y1;
                        })
                        .delay(0)
                        .start();
        } else if (type == 'middle') {
                var coords = { x: Scene_OffsetX, y: Scene_OffsetY, x1: Scene_ScaleX, y1: Scene_ScaleY };
                new TWEEN.Tween(coords)
                        .to({ x: 0, y: 0, x1: 1.0, y1: 1.0 }, 500)
                        .repeat(0)
                        .easing(TWEEN.Easing.Linear.None)
                        .onUpdate(function () {
                                Scene_OffsetX = coords.x;
                                Scene_OffsetY = coords.y;
                                Scene_ScaleX = coords.x1;
                                Scene_ScaleY = coords.y1;
                        })
                        .delay(0)
                        .start();
        } else if (type == 'bottom') {
                var coords = { x: Scene_OffsetX, y: Scene_OffsetY, x1: Scene_ScaleX, y1: Scene_ScaleY };
                new TWEEN.Tween(coords)
                        .to({ x: -ZW_Info.windowWidth * 0.08, y: -ZW_Info.windowWidth * 0.4, x1: 1.2, y1: 1.2 }, 500)
                        .repeat(0)
                        .easing(TWEEN.Easing.Linear.None)
                        .onUpdate(function () {
                                Scene_OffsetX = coords.x;
                                Scene_OffsetY = coords.y;
                                Scene_ScaleX = coords.x1;
                                Scene_ScaleY = coords.y1;
                        })
                        .delay(0)
                        .start();
        }
}


//距离计算公式
function M_distance(a, b) {
        var x = b[0] - a[0],
                y = b[1] - a[1],
                z = b[2] - a[2];
        return Math.sqrt(x * x + y * y + z * z);
};

//角度计算公式
function M_GetAngle(v0, v1, v2) {
        var linev0v1 = M_distance(v0, v1);
        var linev0v2 = M_distance(v0, v2);
        var linev1v2 = M_distance(v1, v2);

        return Math.acos((linev0v1 * linev0v1 + linev1v2 * linev1v2 - linev0v2 * linev0v2) / (2 * linev0v1 * linev1v2)) * 180.0 / 3.1415926;
}

function StartDecorationAnimation(tag, index1, index2, time) {
        //console.log(index1+'++++'+index2);
        if (!time) time = 300;
        var coords = { x: index1 };
        new TWEEN.Tween(coords)
                .to({ x: index2 }, time)
                .repeat(0)
                .easing(TWEEN.Easing.Linear.None)
                .onUpdate(function () {

                })
                .delay(0)
                .start();
}

function GetNodeByTag(tag) {
        for (var i = 0; i < ZW_PicArray.length; i++) {
                if (ZW_PicArray[i].tag == tag) {
                        return ZW_PicArray[i];
                }
        }
        return null;
}

//获取椭圆的轨迹点
function GetEllipsePoints(data) {
        var points = [];

        for (var d = 0; d <= data.t; d++) {
                var point = { x: 0, y: 0 };
                var twoPi = Math.PI * 2;
                var deltaAngle = data.aEndAngle - data.aStartAngle;
                var samePoints = Math.abs(deltaAngle) < Number.EPSILON; // ensures that deltaAngle is 0 .. 2 PI

                while (deltaAngle < 0) {
                        deltaAngle += twoPi;
                }

                while (deltaAngle > twoPi) {
                        deltaAngle -= twoPi;
                }

                if (deltaAngle < Number.EPSILON) {
                        if (samePoints) {
                                deltaAngle = 0;
                        } else {
                                deltaAngle = twoPi;
                        }
                }

                if (data.aClockwise === true && !samePoints) {
                        if (deltaAngle === twoPi) {
                                deltaAngle = -twoPi;
                        } else {
                                deltaAngle = deltaAngle - twoPi;
                        }
                }

                var angle = data.aStartAngle + d / data.t * deltaAngle;
                var x = data.aX + data.xRadius * Math.cos(angle);
                var y = data.aY + data.yRadius * Math.sin(angle);

                if (data.aRotation !== 0) {
                        var cos = Math.cos(data.aRotation);
                        var sin = Math.sin(data.aRotation);
                        var tx = x - data.aX;
                        var ty = y - data.aY; // Rotate the point about the center of the ellipse.

                        x = tx * cos - ty * sin + data.aX;
                        y = tx * sin + ty * cos + data.aY;
                }

                point.x = x;
                point.y = y; //return point.set(x, y);

                points.push(point);
        }

        return points;
}


function animation() {
        var times = 0
        var i = setInterval(function () {

                // //console.log('sss');
                // gStage.update();
                //angle++;
                // let contentPicLocalPath = "/img/t1.png"
                // ZW_Ctx.translate(20, 50);
                // ZW_Ctx.rotate(angle * Math.PI / 180);
                // ZW_Ctx.drawImage(contentPicLocalPath, -20, -50, 40, 100)

                // ZW_Ctx.translate(200, 200);
                // ZW_Ctx.rotate(-angle * Math.PI / 180);
                // ZW_Ctx.drawImage(contentPicLocalPath, -50, -50, 100, 100)

                TWEEN.update();

                ZW_PicArray.sort(function (a, b) { return a.renderOrder - b.renderOrder });

                ZW_Ctx.scale(Scene_ScaleX, Scene_ScaleY);
                for (var i = 0; i < ZW_PicArray.length; i++) {
                        if (ZW_PicArray[i].url != '' && ZW_PicArray[i].visible) {

                                if (ZW_PicArray[i].type == 'tip') {
                                        let contentPicLocalPath = ZW_PicArray[i].url;

                                        ZW_Ctx.translate(ZW_PicArray[i].x0 + Scene_OffsetX, ZW_PicArray[i].y0 + Scene_OffsetY);
                                        ZW_Ctx.rotate(ZW_PicArray[i].rot0 * Math.PI / 180);
                                        ZW_Ctx.scale(ZW_PicArray[i].scale0, ZW_PicArray[i].scale0);

                                        ZW_Ctx.translate(ZW_PicArray[i].x, ZW_PicArray[i].y);
                                        ZW_Ctx.drawImage(contentPicLocalPath, -ZW_PicArray[i].scale * ZW_PicArray[i].width / 2,
                                                -ZW_PicArray[i].scale * ZW_PicArray[i].height / 2, ZW_PicArray[i].width * ZW_PicArray[i].scale, ZW_PicArray[i].height * ZW_PicArray[i].scale);

                                        if (ZW_bTipEdit) {
                                                //贴纸画框
                                                ZW_Ctx.setStrokeStyle("#ffffff");
                                                ZW_Ctx.setLineWidth(2.0 / ZW_PicArray[i].scale0);
                                                ZW_Ctx.rect(-ZW_PicArray[i].scale * ZW_PicArray[i].width / 2,
                                                        -ZW_PicArray[i].scale * ZW_PicArray[i].height / 2, ZW_PicArray[i].width * ZW_PicArray[i].scale, ZW_PicArray[i].height * ZW_PicArray[i].scale);
                                                ZW_Ctx.stroke();

                                                //画删除按钮
                                                ZW_Ctx.drawImage(ZW_Close_Url, -ZW_PicArray[i].scale * ZW_PicArray[i].width / 2 - 12.0 / ZW_PicArray[i].scale0,
                                                        -ZW_PicArray[i].scale * ZW_PicArray[i].height / 2 - 12.0 / ZW_PicArray[i].scale0, 24.0 / ZW_PicArray[i].scale0, 24.0 / ZW_PicArray[i].scale0);

                                                //画缩放按钮
                                                ZW_Ctx.drawImage(ZW_Scale_Url, ZW_PicArray[i].width * ZW_PicArray[i].scale - ZW_PicArray[i].scale * ZW_PicArray[i].width / 2 - 12.0 / ZW_PicArray[i].scale0,
                                                        -ZW_PicArray[i].scale * ZW_PicArray[i].height / 2 - 12.0 / ZW_PicArray[i].scale0, 24.0 / ZW_PicArray[i].scale0, 24.0 / ZW_PicArray[i].scale0);

                                                //画旋转按钮
                                                ZW_Ctx.drawImage(ZW_Rot_Url, ZW_PicArray[i].width * ZW_PicArray[i].scale - ZW_PicArray[i].scale * ZW_PicArray[i].width / 2 - 12.0 / ZW_PicArray[i].scale0,
                                                        ZW_PicArray[i].height * ZW_PicArray[i].scale - ZW_PicArray[i].scale * ZW_PicArray[i].height / 2 - 12.0 / ZW_PicArray[i].scale0, 24.0 / ZW_PicArray[i].scale0, 24.0 / ZW_PicArray[i].scale0);
                                        }

                                        ZW_Ctx.translate(-ZW_PicArray[i].x, -ZW_PicArray[i].y);

                                        ZW_Ctx.scale(1.0 / ZW_PicArray[i].scale0, 1.0 / ZW_PicArray[i].scale0);
                                        ZW_Ctx.rotate(-ZW_PicArray[i].rot0 * Math.PI / 180);
                                        ZW_Ctx.translate(-ZW_PicArray[i].x0 - Scene_OffsetX, -ZW_PicArray[i].y0 - Scene_OffsetY);
                                } else {
                                        let contentPicLocalPath = ZW_PicArray[i].url;

                                        ZW_Ctx.translate(ZW_PicArray[i].x + Scene_OffsetX, ZW_PicArray[i].y + Scene_OffsetY);
                                        ZW_Ctx.rotate(ZW_PicArray[i].rot * Math.PI / 180);
                                        ZW_Ctx.drawImage(contentPicLocalPath, -ZW_PicArray[i].scale * ZW_PicArray[i].width / 2,
                                                -ZW_PicArray[i].scale * ZW_PicArray[i].height / 2, ZW_PicArray[i].width * ZW_PicArray[i].scale, ZW_PicArray[i].height * ZW_PicArray[i].scale);
                                        ZW_Ctx.rotate(-ZW_PicArray[i].rot * Math.PI / 180);
                                        ZW_Ctx.translate(-ZW_PicArray[i].x - Scene_OffsetX, -ZW_PicArray[i].y - Scene_OffsetY);
                                }
                        }



                        // if(ZW_PicArray[i].urls.length > 0 && ZW_PicArray[i].bLoaded){
                        //   let contentPicLocalPath = ZW_PicArray[i].url;

                        //   //  jd.showToast({
                        //   //           title: contentPicLocalPath,
                        //   //           icon: 'success',
                        //   //           duration: 1500
                        //   //         })


                        //   ZW_Ctx.translate(ZW_PicArray[i].x + Scene_OffsetX, ZW_PicArray[i].y + Scene_OffsetY);
                        //   ZW_Ctx.rotate(ZW_PicArray[i].rot * Math.PI / 180);
                        //   ZW_Ctx.drawImage(contentPicLocalPath, -ZW_PicArray[i].scale * ZW_PicArray[i].width / 2,
                        //     -ZW_PicArray[i].scale * ZW_PicArray[i].height / 2, ZW_PicArray[i].width * ZW_PicArray[i].scale, ZW_PicArray[i].height * ZW_PicArray[i].scale);
                        //   ZW_Ctx.rotate(-ZW_PicArray[i].rot * Math.PI / 180);
                        //   ZW_Ctx.translate(-ZW_PicArray[i].x - Scene_OffsetX, -ZW_PicArray[i].y - Scene_OffsetY);
                        // }

                }



                ZW_Ctx.draw()
        }, 20)
}

var Current_Tip_Index = 1;

var Current_Tip_Rot = 0;

Component({
        properties: {

        },

        data: {
                baseUrl: app.globalData.baseUrl,

                listerListInfo: [],
                leftValue1: 0,
                canvasWidth: 360,
                canvasHeight: 360
        },

        detached() {
                clearInterval(this.interval);
        },

        attached() {
                this.position = {
                        x: 150,
                        y: 150,
                        vx: 2,
                        vy: 2
                };
                ZW_Ctx = jd.createCanvasContext('canvas')

                ZW_Info = jd.getSystemInfoSync();
                console.log(ZW_Info);
                gIsIphone = ZW_Info.platform == "ios";

                ZW_PixelRatio = ZW_Info.pixelRatio;

                // this.setData({
                //   leftValue: (info.windowWidth-300)/2
                // });

                this.setData({
                        canvasWidth: ZW_Info.windowWidth,
                        canvasHeight: ZW_Info.windowHeight
                });

                // jd.showToast({
                //   title: ZW_Info.windowWidth.toString()+','+ZW_Info.windowHeight.toString(),
                //   icon: 'success',
                //   duration: 1500
                // })

                //AddChain('chain0','https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/lamer-jd/test1.jpg');
                //AddChain('chain0','https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/aj36/20220121/touming_B.png');
                // AddImage('back', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/test/vans/BG1.png', ZW_Info.windowWidth*0.5 , ZW_Info.windowWidth *0.5* 1.026667,
                //   ZW_Info.windowWidth , ZW_Info.windowWidth * 1.026667, 0, 1.0);
                AddBackgroud(1);

                AddImage('hair', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/hair/hair03.png', ZW_Info.windowWidth * 0.5, ZW_Info.windowWidth * 0.85,
                        ZW_Info.windowWidth * 0.5 * 1.4, ZW_Info.windowWidth * 1.4, 0, 1.0, true, 1);

                AddImage('body', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/body/Body01.png', ZW_Info.windowWidth * 0.5, ZW_Info.windowWidth * 0.85,
                        ZW_Info.windowWidth * 0.5 * 1.4, ZW_Info.windowWidth * 1.4, 0, 1.0, true, 2);


                AddImage('bang', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/bangs/bangs03.png', ZW_Info.windowWidth * 0.5, ZW_Info.windowWidth * 0.85,
                        ZW_Info.windowWidth * 0.5 * 1.4, ZW_Info.windowWidth * 1.4, 0, 1.0, true, 3);


                // AddExpression('https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/expression/i/Male01');

                AddExpression('https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/expression/i/Unisex10');


                //  AddImage('exr', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/expression/i/Male01-0.png', ZW_Info.windowWidth * 0.526, ZW_Info.windowWidth * 0.462,
                //   ZW_Info.windowWidth * 0.5333*0.55, ZW_Info.windowWidth * 0.42666*0.55, 0, 1.0,true,20);


                // AddImage('expression', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/expression/expression01.png', ZW_Info.windowWidth * 0.5, ZW_Info.windowWidth * 0.85,
                //   ZW_Info.windowWidth * 0.5 * 1.4, ZW_Info.windowWidth * 1.4, 0, 1.0,true);

                //   AddImage('expression1', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/expression/ex2/female02-0.png', ZW_Info.windowWidth * 0.5, ZW_Info.windowWidth * 0.85,
                //   ZW_Info.windowWidth * 0.5 * 1.4, ZW_Info.windowWidth * 1.4, 0, 1.0,true);


                AddImage('shoe', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/shoes/shoes01.png', ZW_Info.windowWidth * 0.5, ZW_Info.windowWidth * 0.85,
                        ZW_Info.windowWidth * 0.5 * 1.4, ZW_Info.windowWidth * 1.4, 0, 1.0, true, 5);

                AddImage('pant', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/pants/pants01.png', ZW_Info.windowWidth * 0.5, ZW_Info.windowWidth * 0.85,
                        ZW_Info.windowWidth * 0.5 * 1.4, ZW_Info.windowWidth * 1.4, 0, 1.0, true, 6);

                AddImage('cloth', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/jacket/jacket02.png', ZW_Info.windowWidth * 0.5, ZW_Info.windowWidth * 0.85,
                        ZW_Info.windowWidth * 0.5 * 1.4, ZW_Info.windowWidth * 1.4, 0, 1.0, true, 7);

                // AddImage('hat', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/hat/hat01.png', ZW_Info.windowWidth * 0.5, ZW_Info.windowWidth * 0.85,
                //   ZW_Info.windowWidth * 0.5 * 1.4, ZW_Info.windowWidth * 1.4, 0, 1.0,true,8);

                Current_Tip_Index = 1;
                //AddTip('https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/tips/',1,6,629,390,ZW_Info.windowWidth*0.5,ZW_Info.windowWidth*0.5);

                //AddTip('https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/tips/',2,4,480,452,ZW_Info.windowWidth*0.5,ZW_Info.windowWidth*0.5);
                //AddTip('https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/tips/',3,5,460,480,ZW_Info.windowWidth*0.5,ZW_Info.windowWidth*0.5);
                //AddTip('https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/tips/',4,6,480,248,ZW_Info.windowWidth*0.5,ZW_Info.windowWidth*0.5);
                //AddTip('https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/tips/',5,3,480,347,ZW_Info.windowWidth*0.5,ZW_Info.windowWidth*0.5);
                //AddTip('https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/tips/',6,3,387,480,ZW_Info.windowWidth*0.5,ZW_Info.windowWidth*0.5);
                //AddTip('https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/tips/',7,3,402,480,ZW_Info.windowWidth*0.5,ZW_Info.windowWidth*0.5);
                //AddTip('https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/tips/',8,4,480,480,ZW_Info.windowWidth*0.5,ZW_Info.windowWidth*0.5);

                // ZW_Ctx.setStrokeStyle("#00ff00")
                // ZW_Ctx.setLineWidth(5)
                // ZW_Ctx.rect(0, 0, 300, 300)
                // ZW_Ctx.stroke()

                animation();

                // context.setStrokeStyle("#ff0000")
                // context.setLineWidth(2)
                // context.moveTo(160, 100)
                // context.arc(100, 100, 60, 0, 2 * Math.PI, true)
                // context.moveTo(140, 100)
                // context.arc(100, 100, 40, 0, Math.PI, false)
                // context.moveTo(85, 80)
                // context.arc(80, 80, 5, 0, 2 * Math.PI, true)
                // context.moveTo(125, 80)
                // context.arc(120, 80, 5, 0, 2 * Math.PI, true)
                // context.stroke()

                // 网络
                // let contentPicNetPath = "https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/jd_starbucks/remove.png"
                // jd.getImageInfo({
                //   //保存网络图片
                //   src:contentPicNetPath,
                //   success:
                //     function (res) {
                //       //请求成功后将会生成一个本地路径即res.path,然后将该路径缓存到storageKeyUrl关键字中 
                //       // this.globalData.pic = res.path;
                //       console.log("jd.getImageInfo --- ok")
                //       console.log(res)

                //       context.moveTo(0, 0);       //设置起点状态
                //       context.lineTo(700, 700);       //设置末端状态
                //       context.lineWidth = 5;          //设置线宽状态
                //       context.strokeStyle = "#ff00ff";  //设置线的颜色状态
                //       context.stroke();               //进行绘制


                //       context.drawImage(res.path, 100, 100, 100, 100)
                //        context.draw() // 如果不加这句  就画不出remove；如果加上这句，remove会出来，但之前画的内容就没了

                //     }
                // })



        },

        methods: {
                btnclick: function () {
                        SetImageUrl('cloth', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/jacket/jacket01.png');
                },
                btnclick1: function () {
                        SetImageUrl('pant', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/pants/pants02.png');
                },
                btnclick2: function () {
                        SetImageUrl('hat', '');
                },
                btnclick3: function () {
                        SetImageUrl('bang', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/bangs/bangs01.png');
                        SetImageUrl('hair', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/hair/hair01.png');
                        //this.onGetShot();
                },
                btnclick4: function () {
                        SetImageUrl('body', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/body/Body02.png');
                },
                btnclick5: function () {
                        AddExpression('https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/expression/i/Male02');
                },
                btnclick6: function () {
                        AddBackgroud(2);
                },
                btnclick7: function () {
                        RemoveTip(Current_Tip_Index);
                },
                btnclick8: function () {
                        SetEditState(!ZW_bTipEdit);
                },
                btnclick9: function () {
                        var testRot = GetTipRotation(Current_Tip_Index);
                        testRot += 10;
                        SetTipRotation(Current_Tip_Index, testRot);
                },
                btnclick10: function () {
                        var testPos = GetTipPosition(Current_Tip_Index);
                        SetTipPosition(Current_Tip_Index, testPos[0] + 10, testPos[1] + 10);
                },
                btnclick11: function () {
                        var testPos = GetTipScale(Current_Tip_Index);
                        SetTipScale(Current_Tip_Index, testPos * 1.1);
                },
                btnclick12: function () {
                        //var points = GetTipPoints(Current_Tip_Index);
                        this.onGetShot();

                        // jd.navigateBack({
                        //   delta: 1
                        // });
                },
                onUnload: function () {
                        clearInterval(this.interval);
                },
                onTouchStart(e) {
                        //console.log(e);
                        if (gIsIphone) {
                                // ZW_PixelRatio = 1;
                                // e.touches[0].y -= ZW_Info.screenHeight - ZW_Info.windowHeight;
                        }
                        var clickPos = { x: e.touches[0].x, y: e.touches[0].y };

                        ZW_ClickPos = { x: e.touches[0].x, y: e.touches[0].y };

                        // jd.showToast({
                        //   title: e.touches[0].y.toString() +','+ e.touches[0].y.toString(),
                        //   icon: 'success',
                        //   duration: 1500
                        // })
                        var points = GetTipPoints(Current_Tip_Index);

                        var result = PointInTriangle(ZW_ClickPos.x, ZW_ClickPos.y, points[0].x, points[0].y, points[1].x, points[1].y, points[2].x, points[2].y) ||
                                PointInTriangle(ZW_ClickPos.x, ZW_ClickPos.y, points[0].x, points[0].y, points[2].x, points[2].y, points[3].x, points[3].y);

                        var type = GetTipBtnType(Current_Tip_Index, ZW_ClickPos.x, ZW_ClickPos.y);

                        if (type == '') {
                                if (result) {

                                        ZW_MoveType = 'position';
                                        // jd.showToast({
                                        //   title: 'click!!!',
                                        //   icon: 'success',
                                        //   duration: 500
                                        // })
                                }
                        } else {
                                if (type == 'scale') {
                                        ZW_MoveType = 'scale';
                                        // var testPos = GetTipScale(Current_Tip_Index);
                                        // SetTipScale(Current_Tip_Index,testPos*0.9);
                                } else if (type == 'rotation') {
                                        ZW_MoveType = 'rotation';


                                        // var testRot = GetTipRotation(Current_Tip_Index);
                                        // testRot += 10;
                                        // SetTipRotation(Current_Tip_Index,testRot);
                                }
                                else if (type == 'remove') {
                                        RemoveTip(Current_Tip_Index);
                                } else {

                                        // jd.showToast({
                                        //   title: type,
                                        //   icon: 'success',
                                        //   duration: 500
                                        // })
                                }
                        }

                        //ChangeView('bottom');
                        //AddBackgroud(4);

                        // ChangeView('top');
                        // SetImageUrl('body', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/body/body03.png');

                        // SetImageUrl('cloth', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/jacket/jacket01.png');
                        // SetImageUrl('hat', '');


                        setTimeout(() => {
                                ZW_ClickNodeName = '';
                        }, 350);

                        setTimeout(() => {
                                this.longTap(e);
                        }, 500);
                },
                onTouchMove(e) {
                        if (gIsIphone) {
                                // ZW_PixelRatio = 1;
                                // e.touches[0].y -= ZW_Info.screenHeight - ZW_Info.windowHeight;
                        }


                        var clickPos = { x: e.touches[0].x, y: e.touches[0].y };

                        if (ZW_MoveType != '') {
                                var xOffset = clickPos.x - ZW_ClickPos.x;
                                var yOffset = clickPos.y - ZW_ClickPos.y;

                                if (ZW_MoveType == 'position') {
                                        var testPos = GetTipPosition(Current_Tip_Index);
                                        SetTipPosition(Current_Tip_Index, testPos[0] + xOffset, testPos[1] + yOffset);
                                } else if (ZW_MoveType == 'rotation') {
                                        var testPos = GetTipPosition(Current_Tip_Index);

                                        //计算角度
                                        let A = { X: testPos[0], Y: testPos[1] }
                                        let B = { X: clickPos.x, Y: clickPos.y }
                                        let C = { X: ZW_ClickPos.x, Y: ZW_ClickPos.y }
                                        var AB = Math.sqrt(Math.pow(A.X - B.X, 2) + Math.pow(A.Y - B.Y, 2));
                                        var AC = Math.sqrt(Math.pow(A.X - C.X, 2) + Math.pow(A.Y - C.Y, 2));
                                        var BC = Math.sqrt(Math.pow(B.X - C.X, 2) + Math.pow(B.Y - C.Y, 2));
                                        var cosA = (
                                                Math.pow(AB, 2) + Math.pow(AC, 2) - Math.pow(BC, 2)
                                        ) / (
                                                        2 * AB * AC
                                                );
                                        var angleA = Math.round(Math.acos(cosA) * 180 / Math.PI);


                                        //计算是否为顺时针
                                        var bAdd = true;

                                        var p1 = { x: ZW_ClickPos.x, y: ZW_ClickPos.y };
                                        var p2 = { x: testPos[0], y: testPos[1] };
                                        var p3 = { x: clickPos.x, y: clickPos.y };
                                        bAdd = ((p1.x - p3.x) * (p2.y - p3.y) - (p1.y - p3.y) * (p2.x - p3.x)) < 0;

                                        var testRot = GetTipRotation(Current_Tip_Index);
                                        if (bAdd) {
                                                testRot += angleA;
                                        } else {
                                                testRot -= angleA;
                                        }

                                        SetTipRotation(Current_Tip_Index, testRot);


                                        // if(xOffset > 0 )
                                        // {
                                        //   var testRot = GetTipRotation(Current_Tip_Index);
                                        //   testRot -= angleA;
                                        //   SetTipRotation(Current_Tip_Index,testRot);
                                        // }else
                                        // {
                                        //   var testRot = GetTipRotation(Current_Tip_Index);
                                        //   testRot -= angleA;
                                        //   SetTipRotation(Current_Tip_Index,testRot);
                                        // }

                                } else if (ZW_MoveType == 'scale') {
                                        var testPos = GetTipScale(Current_Tip_Index);
                                        if (xOffset > 0) {
                                                SetTipScale(Current_Tip_Index, testPos * 1.05);
                                        } else {
                                                SetTipScale(Current_Tip_Index, testPos * 0.95);
                                        }

                                }
                        }

                        ZW_ClickPos = { x: e.touches[0].x, y: e.touches[0].y };

                        //console.log(e.touches[0].x / ZW_PixelRatio - ZW_ClickPos.x);

                        //ZW_CurrentPos += (e.touches[0].x - ZW_ClickPos.x) ;


                        //ZW_ClickPos.x = e.touches[0].x / ZW_PixelRatio;

                        // if (ZW_HoldNodeName != '') {
                        //   var clickPos = { x: e.touches[0].x / ZW_PixelRatio, y: e.touches[0].y / ZW_PixelRatio };

                        //   //var resultIndex = -1;
                        //   var resultDis = 100000;

                        //   for (var i = 0; i < ZW_PointsArray[0].length; i++) {
                        //     var dis = M_distance([ZW_PointsArray[0][i].x, ZW_PointsArray[0][i].y, 0], [clickPos.x, clickPos.y, 0]);
                        //     if (dis < resultDis) {
                        //       resultDis = dis;
                        //       ZW_HoldIndex = i;
                        //     }
                        //   }
                        // }
                },
                onTouchEnd(e) {
                        //console.log(e);

                        ZW_MoveType = '';

                        ZW_bHold = false;
                        if (ZW_HoldNodeName != '') {
                                ReleaseProcess();
                                ZW_HoldNodeName = '';
                        }
                },
                onTouchCancel(e) {
                        //console.log(e);
                },
                longTap(e) {

                        if (!ZW_bHold) return;
                        // console.log(e);
                        if (gIsIphone) {
                                ZW_PixelRatio = 1;
                                //e.touches[0].y -= ZW_Info.screenHeight-ZW_Info.windowHeight;
                        }

                },

                onGetShot() {
                        // 将生成的canvas图片，转为真实图片
                        //console.log("onGetShot() ----------------------");
                        jd.canvasToTempFilePath({
                                x: 0,
                                y: 0,
                                width: ZW_Info.windowWidth,
                                height: ZW_Info.windowWidth * 1.76,
                                canvasId: 'canvas',
                                success: function (resp) {
                                        jd.saveImageToPhotosAlbum({
                                                filePath: resp.tempFilePath,
                                                success(res) {
                                                        jd.showToast({
                                                                title: '保存成功',
                                                                icon: 'success',
                                                                duration: 1500
                                                        })
                                                }
                                        })
                                },
                                fail: function (res) {
                                }
                        });
                        return;
                        if (gIsIphone) {
                                Scene_OffsetX = -(ZW_Info.windowWidth - 300) / 2;

                                for (var i = 0; i < ZW_PicArray.length; i++) {
                                        let contentPicLocalPath = ZW_PicArray[i].url;


                                        ZW_Ctx.translate(ZW_PicArray[i].x + Scene_OffsetX, ZW_PicArray[i].y + Scene_OffsetY);
                                        ZW_Ctx.rotate(ZW_PicArray[i].rot * Math.PI / 180);
                                        ZW_Ctx.drawImage(contentPicLocalPath, -ZW_PicArray[i].scale * ZW_PicArray[i].width / 2,
                                                -ZW_PicArray[i].scale * ZW_PicArray[i].height / 2, ZW_PicArray[i].width * ZW_PicArray[i].scale, ZW_PicArray[i].height * ZW_PicArray[i].scale);
                                        ZW_Ctx.rotate(-ZW_PicArray[i].rot * Math.PI / 180);
                                        ZW_Ctx.translate(-ZW_PicArray[i].x - Scene_OffsetX, -ZW_PicArray[i].y - Scene_OffsetY);
                                }

                                ZW_Ctx.draw()

                                jd.canvasToTempFilePath({
                                        //x: (ZW_Info.windowWidth-300)/2,
                                        x: 0,
                                        y: 0,
                                        width: 300,
                                        height: 300,
                                        canvasId: 'canvas',
                                        success: function (resp) {
                                                //console.log(resp)
                                                Scene_OffsetX = 0;
                                                jd.saveImageToPhotosAlbum({
                                                        filePath: resp.tempFilePath,
                                                        success(res) {
                                                                jd.showToast({
                                                                        title: '保存成功',
                                                                        icon: 'success',
                                                                        duration: 1500
                                                                })
                                                        }
                                                })
                                        },
                                        fail: function (res) {
                                        }
                                })
                        } else {
                                jd.canvasToTempFilePath({
                                        x: (ZW_Info.windowWidth - 300) / 2,
                                        y: 0,
                                        width: 300,
                                        height: 300,
                                        canvasId: 'canvas',
                                        success: function (resp) {
                                                //console.log(resp)
                                                jd.saveImageToPhotosAlbum({
                                                        filePath: resp.tempFilePath,
                                                        success(res) {
                                                                jd.showToast({
                                                                        title: '保存成功',
                                                                        icon: 'success',
                                                                        duration: 1500
                                                                })
                                                        }
                                                })
                                        },
                                        fail: function (res) {
                                        }
                                })
                        }

                }
        },
})